/**
 *  ward @ 2019
 *  harrydeep@wardyan.top
 */
package top.wardyan.po;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

import top.wardyan.base.config.Const;
import top.wardyan.base.util.swt.Msg;

/**
 * 带有一个标签,一个Text输入框的控件 
 * @author ward 2019年2月24日
 */
public class WTextInput{

	/** 左边标签 **/
	protected WLabel	label;
	/** 右边输入框 **/
	protected WText	text;
	/** 设置是否只能输入数字. 默认false, 允许输入数字或字符串 **/
	protected Boolean	isNumber = false;
	
	protected WTextInput self;
	// bounds
	protected Integer x;
	protected Integer y;
	protected Integer width;
	protected Integer height;
	
	//protected Integer	labelWidth;
	/** label的宽度比例 **/
	protected Float	labelWidthRate = 0.3F;
	/** 标签跟输入框之间的距离 **/
	protected Integer	padding = 5;
	protected String	defValue;
	protected Font		labelFont = Const.FONT_H7;
	protected Font		textFont = Const.FONT_H8;
	protected Color	labelBgColor = Const.COLOR_WHITE;
	protected WComposite desk;
	protected Integer	maxLength = 64;
	protected Rectangle	bound;
	//protected
	protected Point	bottom;
	protected Point	location;
	protected Point	size;
	
	
	// bound: 这是整个控件的大小, 至于标签会按比例计算
	public WTextInput(WComposite parent, Rectangle bound, String label, Integer maxLength) {
		this(parent, bound, label, maxLength, null);
	}
	
	public WTextInput(WComposite parent, Rectangle bound, String label, Integer maxLength, Point labelSize) {
		super();
		if(maxLength == null || maxLength < 1)
			maxLength = this.maxLength;
		self = this;
		this.bound = bound;
		this.size = new Point(bound.width, bound.height);
		this.location = new Point(bound.x, bound.y);
		
		desk = new WComposite(parent, bound);
		desk.setBounds(bound);
		Rectangle labelBound;
		if(labelSize != null) {
			labelBound = new Rectangle(0, 0, labelSize.x, labelSize.y);
		}else {
			labelBound = new Rectangle(0, 0, (int) (bound.width * labelWidthRate), bound.height);
		}
		this.label = new WLabel(	desk, 
									labelBound, 
									labelFont, 
									label, 
									SWT.RIGHT);
		
		this.text = new WText(		desk, 
									new Rectangle(this.label.getBottom().x + 5, 0, bound.width - this.label.getSize().x - 5, bound.height * 8 / 10), 
									textFont);
		this.text.setTextLimit(maxLength);
		this.text.setToolTipText(label + ",最长:" + maxLength.toString());
		this.text.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				// 遍历每一个字符检查是否超过范围
				if(self.isNumber)
					for(char c : self.text.getTextChars()) {
						if((c < 48 || c > 57) && c != '.') {
							Msg.showMessage("错误", "只允许输入数字");
							self.text.setText(self.text.getText().replace(String.valueOf(c), ""));
							//self.text.setFocus();
							self.text.setSelection(self.text.getText().length(), self.text.getText().length());
						}
					}
			}
		});
		
		
		this.setBottom();
	}

	public Boolean getIsNumber() {
		return isNumber;
	}

	public WTextInput setIsNumber(Boolean isNumber) {
		this.isNumber = isNumber;
		return this;
	}
	
	
	protected void setBottom() {
		this.bottom = new Point(this.bound.x + this.bound.width, this.bound.y + this.bound.height);
	}
	public Point getBottom() {
		return this.bottom;
	}

	public Point getLocation() {
		return location;
	}

	public Point getSize() {
		return size;
	}
	
	public String getText() {
		return text.getText();
	}
	
	public WTextInput setText(String text) {
		this.text.setText(text);
		return this;
	}
	
	public Point getLabelSize() {
		return this.label.getSize();
	}
	
	
	
	public Point getTextSize() {
		return this.text.getSize();
	}
	
	
}
